/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import business.CarBusiness;
import business.LogicBusiness;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import model.Car;
import model.CarInCart;

/**
 *
 * @author khanhanh
 */
public class CarBean {

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    private CarBusiness carBiz;
    private Car carDetails;
    private List<CarInCart> cartCar;
    private float total;
    private float priceDriver = 100; //test
    private LogicBusiness logicBiz = new LogicBusiness();

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }

    public List<CarInCart> getCartCar() {
        return cartCar;
    }

    public void setCartCar(List<CarInCart> cartCar) {
        this.cartCar = cartCar;
    }

    public Car getCarDetails() {
        return carDetails;
    }

    /** Creates a new instance of CarBean */
    public CarBean() {
        carBiz = new CarBusiness();
        cartCar = new ArrayList<CarInCart>();

    }

    public String actionGetCarDetails(int carID) {
        carDetails = carBiz.getCarByID(carID);
        return "cardetail.xhtml";
    }

    public String actionOrderCar(int carID) {
        Car c = carBiz.getCarByID(carID);
        boolean flag = false;
        CarInCart cIC = new CarInCart(carID, c.getCarName(), 1, c.getPrice(), c.getCarImg(), false, new Date(), new Date(), c.getPrice());
        for (CarInCart carInCart : cartCar) {
            if (carInCart.getCarID() == carID) {
                carInCart.setQuantity(carInCart.getQuantity() + 1);
                flag = true;
                break;
            }
        }
        if (!flag) {
            cartCar.add(cIC);
        }        
        total += cIC.getPrice();
        return "ordercars.xhtml";
    }

    public String actionUpdateCart() {
        total = 0;
        int tempDate;
        for (CarInCart carInCart : cartCar) {
            tempDate = logicBiz.dateDiff(carInCart.getPickUp(), carInCart.getDropOff());
            if (carInCart.isDriver()) {
                carInCart.setTotalPerCar((priceDriver * tempDate * carInCart.getQuantity()) + (carInCart.getPrice() * tempDate * carInCart.getQuantity()));
                total += (priceDriver * tempDate * carInCart.getQuantity()) + (carInCart.getPrice() * tempDate * carInCart.getQuantity());
            } else {
                carInCart.setTotalPerCar(carInCart.getPrice() * tempDate * carInCart.getQuantity());
                total += carInCart.getPrice() * tempDate * carInCart.getQuantity();
            }
        }
        return "ordercars.xhtml";
    }

    public String actionRemoveCarInCart(int carIDinCart) {
        for (CarInCart carInCar : cartCar) {
            if (carInCar.getCarID() == carIDinCart) {
                cartCar.remove(carInCar);
                total -= carInCar.getTotalPerCar();
                break;
            }
        }
        return "ordercars.xhtml";
    }

    public String actionReview() {
        return "reviewcarrental";
    }
}
